package com.tensquare.web.filter;
/*
 *ClassName:ManageFilter
 *Description:Zuul过滤器类,前端网关的过滤器
 *@author:可爱的大鸭子
 */

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import util.JwtUtil;

@Component
public class WebFilter extends ZuulFilter {
    /*pre为前置过滤器，post为后置过滤器，route在路由请求时被调用*/
    @Override
    public String filterType() {
        return "pre";
    }

    /*过滤器的优先级，值越小，优先级越高*/
    @Override
    public int filterOrder() {
        return 0;
    }

    /*是否执行该过滤器，true表示要过滤*/
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /*过滤当前执行操作的人是不是用户user*/
    @Override
    public Object run() throws ZuulException {
        System.out.println("前端管理zuul过滤器...");
        //向header中添加鉴权令牌
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String authHeader = (String)request.getHeader("X-Token");
        if(authHeader!=null ){
            requestContext.addZuulRequestHeader("X-Token",authHeader);
            System.out.println("二次头："+authHeader);
        }
        return null;
    }
}
